package com.fcscs.cservice.domain;

import java.util.Comparator;
import java.util.List;

import com.fcscs.core.domain.HistoricalEntity;
import com.fcscs.cservice.common.CServiceCommonEnums.NotifDestType;
import com.fcscs.cservice.common.CServiceCommonEnums.NotifType;

public class NotifDestination extends HistoricalEntity {

	private static final long serialVersionUID = 1174338142797866070L;

	private NotifType notifType;
	private NotifDestType destType;
	private String destination;
	private String parameters;

	public NotifDestination() {
	}

	@Override
	public DBLoc getDBLoc() {
		return DBLoc.GUEST;
	}

	public void setNotifTypeEnum(NotifType notifType) {
		this.notifType = notifType;
	}

	public NotifType getNotifTypeEnum() {
		return notifType;
	}

	public void setNotifType(String notifType) {
		this.notifType = notifType == null ? null : NotifType.valueOf(notifType);
	}

	public String getNotifType() {
		return notifType == null ? null : notifType.toString();
	}

	public void setDestTypeEnum(NotifDestType destType) {
		this.destType = destType;
	}

	public NotifDestType getDestTypeEnum() {
		return destType;
	}

	public void setDestType(String destType) {
		this.destType = destType == null ? null : NotifDestType.valueOf(destType);
	}

	public String getDestType() {
		return destType == null ? null : destType.toString();
	}

	public void setDestination(String destination) {
		this.destination = destination;
	}

	public String getDestination() {
		return destination;
	}

	public void setParameters(String parameters) {
		this.parameters = parameters;
	}

	public String getParameters() {
		return parameters;
	}

	public static class NotifDestinationComparator implements Comparator<NotifDestination> {

		@Override
		public int compare(NotifDestination o1, NotifDestination o2) {
			int result = o1.getNotifTypeEnum().compareTo(o2.getNotifTypeEnum());
			return result;
		}
	}

	private static Comparator<NotifDestination> comparator = new NotifDestinationComparator();

	public static Comparator<NotifDestination> getComparator() {
		return comparator;
	}

	/**
	 * the list should be sorted with FieldEditabilityComparator.comparator
	 * @param list
	 * @param category
	 * @param field
	 * @return
	 */
    public static int binarySearch(List<NotifDestination> list, NotifType type) {
    	int low = 0;
    	int high = list.size()-1;

    	while (low <= high) {
    	    int mid = (low + high) >>> 1;
    	    NotifDestination midVal = list.get(mid);
    	    int cmp = midVal.getNotifTypeEnum().compareTo(type);

    	    if (cmp < 0)
    		low = mid + 1;
    	    else if (cmp > 0)
    		high = mid - 1;
    	    else
    		return mid; // key found
    	}
    	return -(low + 1);  // key not found
    }

    @Override
    public String toString() {
    	return notifType.toString() + "-" + destType.toString() + ":" + destination;
    }
}
